package cn.zyzpp.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * Create by yster@foxmail.com 2018/5/22/022 19:18
 */
public class FileUtil {
    public final static String filePath = ProjectPath.getRootPath("/userImg/");

    public static String uploadFile(MultipartFile file,int uid) throws Exception {
        String path = filePath+ uid +".png";
        File targetFile = new File(path);
        if(targetFile.exists()){
            targetFile.delete();
        }
        FileOutputStream out = new FileOutputStream(path);
        out.write(file.getBytes());
        out.flush();
        out.close();
        return path;
    }

    /**
     * 读取文件字节流
     * @param fileName
     * @return
     * @throws IOException
     */
    public static byte[] getImg(String fileName) throws IOException {
        File file = new File(fileName);
        FileInputStream inputStream = new FileInputStream(file);
        byte[] data = new byte[(int)file.length()];
        int length = inputStream.read(data);
        inputStream.close();
        return data;
    }

}
